home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_mac.hqx / SRGP port to 5.0 (compressed) / SRGP_SPHIGS Root / MacSPHIGS / sph_poly.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-13  |  2.4 KB  |  94 lines

  1. #include "HEADERS.h"
  2. #include <stdio.h>
  3. #include "sphigslocal.h"
  4. #include <string.h>
  5.  
  6. #ifdef THINK_C
  7. #include <stdlib.h>
  8. #endif
  9.  
  10. #include "sph_poly.proto.h"
  11.  
  12. /** POLYHEDRON
  13. The routine in this file creates the actual polyhedron record.
  14. The structure database points to the memory allocated here.
  15. **/
  16.  
  17.  
  18.  
  19.  
  20. void
  21. SPH__freePolyhedron (POLYHEDRON *poly)
  22. {
  23.    facet *fptr = poly->facet_list;
  24.    int fcount = poly->facet_count;
  25.  
  26.    free (poly->vertex_list);
  27.  
  28.    while (fcount--) {
  29.       free (fptr->vertex_indices);
  30.       fptr = fptr++;
  31.    }
  32.  
  33.  
  34.    free (poly->facet_list);
  35.    free (poly);
  36. }
  37.  
  38.  
  39.  
  40.  
  41. /*!*/
  42. void
  43. SPH_polyhedron
  44.    (int numverts, int numfacets, point *verts, vertex_index *facets)
  45. {
  46.    register i;
  47.    register vertex_index *viptr;
  48.    vertex_index *start_viptr;
  49.    facet *fptr;
  50.    POLYHEDRON *newpoly;
  51.    vector vec1, vec2;
  52.  
  53.  
  54.    ALLOC_RECORDS (newpoly, POLYHEDRON, 1);
  55.    ALLOC_RECORDS (newpoly->vertex_list, MAT3hvec, numverts);
  56.    ALLOC_RECORDS (newpoly->facet_list, facet, numfacets);
  57.  
  58.    newpoly->vertex_count = numverts;
  59.    newpoly->facet_count = numfacets;
  60.  
  61.    /* COPY VERTICES, TRANSFORMING TO HVERTS. */
  62.    for (i=0; i<numverts; i++)
  63.       MAT3_SET_HVEC (newpoly->vertex_list[i] ,
  64.              verts[i][0], verts[i][1], verts[i][2], 1.0);
  65.  
  66.    /* SEPARATE LONG LIST OF VERTEX INDICES INTO INDIVIDUAL FACET DEF'S */
  67.    fptr = newpoly->facet_list;
  68.    viptr = facets;
  69.    while ((numfacets--) > 0) {
  70.       /* Calculate number of vertex indices in the next list */
  71.       start_viptr = viptr;
  72.       while (*(++viptr) != (-1));   /* scan forward to find next sentinel */
  73.       fptr->vertex_count = viptr - start_viptr;
  74.  
  75.       /* Allocate space for them, and copy into the new memory */
  76.       ALLOC_RECORDS (fptr->vertex_indices, vertex_index, fptr->vertex_count);
  77.       bcopy (start_viptr, fptr->vertex_indices, 
  78.              sizeof(vertex_index) * fptr->vertex_count);
  79.  
  80.       /* Calculate normal */
  81.       MAT3_SUB_VEC (vec1, newpoly->vertex_list[fptr->vertex_indices[1]],
  82.                    newpoly->vertex_list[fptr->vertex_indices[0]]);
  83.       MAT3_SUB_VEC (vec2, newpoly->vertex_list[fptr->vertex_indices[2]],
  84.                    newpoly->vertex_list[fptr->vertex_indices[1]]);
  85.       MAT3cross_product (fptr->normal, vec1, vec2);
  86.       fptr->normal[3] = 1.0;
  87.             
  88.       viptr++;   /* to move past sentinel to start of next list */
  89.       fptr++;    /* to move to next facet struct in the array of facets */
  90.    }
  91.    
  92.    SPH__add_polyhedron_element (newpoly);
  93. }
  94.